﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Threading;
using System.Web;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
using Test.MVC;

namespace Test.Web
{
    public class MvcApplication : HttpApplicationBase //System.Web.HttpApplication
    {
        //protected void Application_Start()
        //{
        //    AreaRegistration.RegisterAllAreas();
        //    FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        //    RouteConfig.RegisterRoutes(RouteTable.Routes);
        //    BundleConfig.RegisterBundles(BundleTable.Bundles);
        //}


        /// <summary>
        /// 当前是否在DEBUG模式
        /// </summary>
        public static bool IsDebug { get; private set; }

        [Conditional("DEBUG")]
        public static void SetDebugParameter()
        {
            IsDebug = true;
        }

        public override void Application_BeginRequest(object sender, EventArgs e)
        {
            Thread.CurrentThread.CurrentCulture = new CultureInfo("zh-cn");
            base.Application_BeginRequest(sender, e);
        }

        public override void Application_EndRequest(object sender, EventArgs e)
        {
            base.Application_EndRequest(sender, e);

            #region 缓存控制

            string[] needCacheExtension = { ".js", ".css", ".png", ".jpg", ".eot", ".svg", ".ttf", ".woff" };
            if (needCacheExtension.Contains(Request.CurrentExecutionFilePathExtension))
            {
                Response.Cache.SetExpires(DateTime.Now.AddDays(7).AddHours(1));
            }

            #endregion
        }

        public override void Application_Start(object sender, EventArgs e)
        {
            base.Application_Start(sender, e);

            //判断是调试还是产品环境
            SetDebugParameter();

            //给域名对象赋值，顺便缓存配置
            //自定义ModelBunder
            //ModelBinders.Binders.DefaultBinder = new FishcooModelBinder();

            AreaRegistration.RegisterAllAreas();
            //GlobalConfiguration.Configure(WebApiConfig.Register);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);

        }
    }
}
